﻿using UnityEngine;

public static partial class Extensions
{
    /// <summary>
    /// 重设透明度
    /// </summary>
    public static Color AlphaModified(this Color color, float a)
    {
        return new Color(color.r, color.g, color.b, a);
    }

    /// <summary>
    /// 从RGBA生成颜色
    /// </summary>
    public static Color ToColor(this string RGBAString)
    {
        if (!ColorUtility.TryParseHtmlString(RGBAString, out var color))
        {
            Debug.LogError($"颜色转换失败：错误的颜色字符串 {RGBAString}，正确格式应为 #{Color.white.ToRGBAString()}");
            return Color.white;
        }
        return color;
    }

    /// <summary>
    /// 生成颜色的RGB字串
    /// </summary>
    public static string ToRGBString(this Color color)
    {
        return ColorUtility.ToHtmlStringRGB(color);
    }

    /// <summary>
    /// 生成颜色的RGBA字串
    /// </summary>
    public static string ToRGBAString(this Color color)
    {
        return ColorUtility.ToHtmlStringRGBA(color);
    }
}